/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var LayoutFrame = new Class({
    
    Implements: [Options,Events],
    
    options: {
        id  : '',
        url : '',
        html: ''
    },
    
    dataFrames   : new Array(),
    hasSibling   : false,    
    
    initialize: function(options){
        this.setOptions(options);
    },
    
    getId: function(){
        return this.options.id;
    },
    
    load: function(el){
        if(this.options.html.length > 0){
            var insertHTML = function(){
                el.set('html', this.options.html);
                this.fireEvent('contentLoaded',[this]);
            };
            insertHTML.delay(50,this);
        }else{
            var HTMLRequest = new Request.HTML({
                url: this.options.url,
                onSuccess : function(responseTree, responseElements, responseHTML, responseJavaScript){                 
                    el.set('html', responseHTML);
                    this.options.html = responseHTML;
                    this.fireEvent('contentLoaded',[this]);
                }.bind(this)
            }).get({'ajax' : true});
        }
    },
    
    render: function(el){
        Array.each(this.dataFrames, function(dataFrame,key){  
            dataFrame.removeEvents();
            dataFrame.addEvent('timelineEnd', function(dataFrame){
                //dataFrame.removeEvents('timelineEnd');
                //console.log('isMaster:'+dataFrame.isMaster());
                //console.log('hasSiblig:'+this.hasSibling);
                if(dataFrame.isMaster() && this.hasSibling){                                     
                    this.fireEvent('timelineEnd',[this]);
                }else{
                    dataFrame.startLoop();
                }               
            }.bind(this));
            dataFrame.startLoop();
        }.bind(this));
    },
    
    bindDataFrames: function(dataFrames){      
        Array.each(dataFrames, function(dataFrame,key){
            if($(dataFrame.getHtmlId())){
                dataFrame.prepareLoop();
                this.dataFrames[this.dataFrames.length+1] = dataFrame;
            }
        }.bind(this));  
    },
    
    unbindDataFrames: function(){
        Array.each(this.dataFrames, function(dataFrame,key){
            dataFrame.stopLoop();
            //dataFrame.removeEvents();
        });
        this.dataFrames = new Array();
    },
    
    setHasSibling: function(value){
        this.hasSibling = value;
    }
});


